package me.servlet;

import me.domain.Student;
import me.service.StudentService;
import me.service.impl.StudentServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    //实例化StudentService对象
//    private StudentService studentService = new StudentServiceImpl();
    //使用工厂创建StudentService对象
//    private StudentService studentService = FactoryServiceImpl.getInstance().createStudentService();
    //通过工厂类获取StudentService对象 使用多例  如果需要单例需要写在方法内部
//    private StudentService studentService = (StudentService) Factory.getBeans("studentService");

    //通过spring容器获取容器对象
    ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    //通过容器获取studentDao对象
    StudentService studentService = ac.getBean("studentService", StudentServiceImpl.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取param参数，判断是增删查改的哪一个
        String param = request.getParameter("param");
        //作比较的时候要把常量写在前面，以防止空指针异常
        if("add".equals(param)){
            //如果是添加
            //获取添加的信息
            long id = Long.parseLong(request.getParameter("id"));
            String name = request.getParameter("name");
            String gender = request.getParameter("gender");
            int age = Integer.parseInt(request.getParameter("age"));
            String address = request.getParameter("address");
            //将信息封转成Student对象
            Student student = new Student(id,name,gender,age,address);
            //调用添加方法，将学生信息添加到数据库中
            studentService.addStudent(student);
            //添加完毕返回主页面
            response.sendRedirect("indexServlet");
        }else if("modify".equals(param)){
            //如果是准备修改
            //获取前台传递过来的ID
            long id = Long.parseLong(request.getParameter("id"));
            //通过ID查找一个学生信息
            Student student = studentService.findStudentById(id);
            //将信息封转到request中，传递给修改页面，为什么不给session，因为我想给request
            request.setAttribute("student",student);
            //重定向到update.jsp页面
            request.getRequestDispatcher("update.jsp").forward(request,response);
        }else if("update".equals(param)){
            //如果是修改
            //获取修改的对象属性
            long id = Long.parseLong(request.getParameter("id"));
            String name = request.getParameter("name");
            String gender = request.getParameter("gender");
            int age = Integer.parseInt(request.getParameter("age"));
            String address = request.getParameter("address");
            //将信息封转成Student对象
            Student student = new Student(id,name,gender,age,address);
            //调用修改方法
            studentService. updateStudent(student);
            //修改完毕返回主页面
            response.sendRedirect("indexServlet");
        }else if("delete".equals(param)){
            //获取ID
            long id = Long.parseLong(request.getParameter("id"));
            //通过ID删除学生信息
            studentService.deleteStudentById(id);
            //删除完毕返回首页
            response.sendRedirect("indexServlet");
        }else {
            System.out.println("也许出了一些问题");
        }
    }
}
